package com.blockmeta.bbs.immodule.widget.chatboard;

import android.content.Context;
import android.net.Uri;
import com.blankj.utilcode.util.LogUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.immodule.R;

import java.util.ArrayList;
import java.util.List;

import static com.blockmeta.bbs.immodule.Constants.CALL_CHAR;

public class InputFieldQueryAdapter extends ArrayAdapter<InputFieldQueryItem>
{
	static class ViewHolder
	{
		TextView m_tvTitle, m_tvDetail;
		ImageView m_imgPhoto;
		ImageView m_imgTick;
	}

	public enum InputQueryItemMode
	{
		SIMPLE, DETAIL, DETAIL_MULTI;
	}

	private InputQueryItemMode m_mode = InputQueryItemMode.SIMPLE;
	private int m_nDefaultIconRes = 0;
	private ArrayList<Integer> m_listPicked;
	private int m_nPhotoSize = -1;

	private int getPhotoSize()
	{
		if (m_nPhotoSize <= 0)
		{
			m_nPhotoSize = (int) DimentionsUtil.dp2px(40);
		}
		return m_nPhotoSize;
	}

	public InputFieldQueryAdapter(Context context, List<InputFieldQueryItem> list, int defaultIconRes)
	{
		super(context, 0, list);
		m_nDefaultIconRes = defaultIconRes;
		m_listPicked = new ArrayList<Integer>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		final ViewHolder holder;
		if (convertView == null)
		{
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.share_control_input_query_item, null);
			holder = new ViewHolder();
			holder.m_tvTitle = (TextView) convertView.findViewById(R.id.input_query_item_title_tv);
			holder.m_tvDetail = (TextView) convertView.findViewById(R.id.input_query_item_detail_tv);
			holder.m_imgPhoto = (ImageView) convertView.findViewById(R.id.input_query_item_photo_img);
			holder.m_imgTick = (ImageView) convertView.findViewById(R.id.input_query_item_img_tick);
			convertView.setTag(holder);
		}
		fill(convertView, position);
		return convertView;
	}

	private void fill(View view, int position)
	{
		final InputFieldQueryItem tItem = getItem(position);
		final ViewHolder holder = (ViewHolder) view.getTag();
		holder.m_tvTitle.setText(tItem.getTitle());
		if (m_mode == InputQueryItemMode.SIMPLE)
		{
			holder.m_tvDetail.setVisibility(View.GONE);
			holder.m_imgPhoto.setVisibility(View.GONE);
			holder.m_imgTick.setVisibility(View.INVISIBLE);
		}
		else if (m_mode == InputQueryItemMode.DETAIL || m_mode == InputQueryItemMode.DETAIL_MULTI)
		{
			holder.m_tvDetail.setVisibility(View.VISIBLE);
			holder.m_imgPhoto.setVisibility(View.VISIBLE);

			if (m_mode == InputQueryItemMode.DETAIL_MULTI)
			{
				holder.m_imgTick.setVisibility(View.VISIBLE);
				if (m_listPicked.contains(new Integer(position)))
				{
					holder.m_imgTick.setImageResource(R.drawable.select_done);
				}
				else
				{
					holder.m_imgTick.setImageResource(R.drawable.select01);
				}
			}
			else
			{
				holder.m_imgTick.setVisibility(View.INVISIBLE);
			}
						
			holder.m_tvDetail.setText(tItem.getDetail());
			
			if (CALL_CHAR.equals(tItem.getType()))
			{
				holder.m_tvDetail.setVisibility(View.GONE);
			}

			String tServingURL = tItem.getImgUrl();
			if (tServingURL == null || tServingURL.equals("") || tServingURL.equals("null"))
			{
				holder.m_imgPhoto.setImageURI(Uri.parse(""));
			}
			else
			{

				LogUtils.d("tingim",tServingURL + "tag " + holder.m_imgPhoto.getTag());
				if(!tServingURL.equals(holder.m_imgPhoto.getTag())){
					holder.m_imgPhoto.setTag(tServingURL);
					LogUtils.d("tingim","@load img " + tServingURL);
					LoadPicUtil.loadWithPicasso(holder.m_imgPhoto, tServingURL);
				}
			}
		}
	}
	
	public ArrayList<Integer> getPicked()
	{
		return m_listPicked;
	}
	
	public InputQueryItemMode getMode()
	{
		return m_mode;
	}

	public void setMode(InputQueryItemMode pMode)
	{
		m_mode = pMode;
	}
}
